package com.sgb.kjwl.view.ui.adapter.packers;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.makeramen.roundedimageview.RoundedImageView;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.FindPackersEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;

import java.util.List;

/**
*@author 陈方凯
*@date 2020/6/15
*description:
*/
public class FindPackersAdapter extends BaseRecycleAdapter<FindPackersEntity> {

    private RequestManager requestManager;
    private boolean isClass;
    private boolean isHide = false;

    public FindPackersAdapter(Context context, List<FindPackersEntity> data, RequestManager requestManager) {
        super(context, R.layout.item_find_packers, data);
        this.requestManager = requestManager;
    }

    public FindPackersAdapter(Context context, List<FindPackersEntity> data, RequestManager requestManager, boolean isClass, boolean isHide) {
        super(context, R.layout.item_find_packers, data);
        this.requestManager = requestManager;
        this.isClass = isClass;
        this.isHide = isHide;
    }

    /**
     *
     * @param context
     * @param data
     * @param requestManager 图片加载的管理器
     * @param isClass 是否为施工队，施工工队为true，班组工队为false
     */
    public FindPackersAdapter(Context context, List<FindPackersEntity> data, RequestManager requestManager,boolean isClass) {
        super(context, R.layout.item_find_packers, data);
        this.requestManager = requestManager;
        this.isClass = isClass;
    }

    @Override
    public void convert(ItemViewHolder holder, final FindPackersEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        TextView tvPackersTitle = holder.itemView.findViewById(R.id.tv_packers_title);
        RoundedImageView ivPackerAvatar = holder.itemView.findViewById(R.id.iv_packer_avatar);
        LinearLayout lvTeamPerson = holder.itemView.findViewById(R.id.lv_team_person);
        TextView tvTime = holder.itemView.findViewById(R.id.tv_time);
        TextView tvDepartment = holder.itemView.findViewById(R.id.tv_department);
        String department = "";
        if (!isClass){
            if (TextUtils.isEmpty(entity.getAffiliatedUnitPic())){
                department = "暂无隶属单位";
            }else if (entity.getAffiliatedUnitPic().contains("https")){
                department = "暂无隶属单位";
            }else {
                department = entity.getAffiliatedUnitPic();
            }
        }else {
            if (TextUtils.isEmpty(entity.getLegalComName())){
                department = "暂无隶属单位";
            }else if (entity.getLegalComName().contains("https")){
                department = "暂无隶属单位";
            }else {
                department = entity.getLegalComName();
            }
        }
        tvDepartment.setText(department);
        if (isClass){
            if (null != entity.getAvatarAddress() && !TextUtils.isEmpty(entity.getAvatarAddress())) {
                RequestOptions options = new RequestOptions()
                        .placeholder(R.mipmap.construction_team_detail)
                        .transforms(new CenterCrop(), new RoundedCorners(8));
                requestManager.load(entity.getAvatarAddress()).apply(options).into(ivPackerAvatar);
            } else {
                requestManager.load(R.mipmap.construction_team_detail).into(ivPackerAvatar);
            }
        }else {
            if (null != entity.getAvatarAddress() && !TextUtils.isEmpty(entity.getAvatarAddress())) {
                RequestOptions options = new RequestOptions()
                        .placeholder(R.mipmap.professional_crew_detail)
                        .transforms(new CenterCrop(), new RoundedCorners(8));
                requestManager.load(entity.getAvatarAddress()).apply(options).into(ivPackerAvatar);
            } else {
                requestManager.load(R.mipmap.professional_crew_detail).into(ivPackerAvatar);
            }
        }

        if (TextUtils.isEmpty(entity.getTeamNumber()) || "0".equals(entity.getTeamNumber()) || "0人".equals(entity.getTeamNumber())) {
            lvTeamPerson.setVisibility(View.GONE);
        } else {
            lvTeamPerson.setVisibility(View.VISIBLE);
        }
        String[] strArray = entity.getCategory().trim().replaceAll(",","\\|").split("\\|");
        tvTime.setText(String.format("%s 发布", DateUtils.timestampToDate(entity.getCreateTime())));
        tvPackersTitle.setText(String.format("%s-%s-%s",entity.getContact(), strArray[strArray.length - 1] , entity.getTeamType() == 0?"施工队":entity.getTeamType()==1?"班组工队":"承包商"));
        ImageView linView = holder.itemView.findViewById(R.id.iv_tag);
        if (isHide){
            linView.setVisibility(View.GONE);
        }else {
            linView.setVisibility(View.VISIBLE);
        }
    }

}
